package com.sanyang.lawyer.controller.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.ResultApi.CodeMsg;
import com.sanyang.lawyer.ResultApi.ResponseResult;
import com.sanyang.lawyer.entity.*;
import com.sanyang.lawyer.service.client.ClientService;
import com.sanyang.lawyer.service.client.Client_messageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("clientMessage")
public class ClientMessageController {
    
    @Autowired
    private Client_messageService messageService;
    
    @Autowired
    private ClientService clientService;
    
    
    private ObjectMapper objectMapper = new ObjectMapper();
    

    //添加数据
    @RequestMapping("insertOne")
    public ResponseResult<Integer> insertOne(String data){
        Client_message message = null;
        try {
            message = objectMapper.readValue(data, Client_message.class);
        } catch (IOException e) {
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        //先添加客户详细信息表
        messageService.insertOne(message);

        //添加返回主键id
        int messageid= message.getMessageId();
        //创建客户简要信息
        Client client = new Client();
        PackedBean(message,client);
        //添加客户简要信息表
        clientService.insertOne(client);
        //添加返回客户id
        int clientid = client.getClientId();
        return ResponseResult.success(clientid);
    }
    
    //客户详情转换为客户简要信息
    public void PackedBean(Client_message message ,Client client){
        client.setClientName(message.getName());
        client.setClientPhone(message.getPhone());
        client.setClientRegion(message.getRegion());
        client.setClientAssign("未");
        client.setClientSave(new Date());
        //使用当前登录用户的id 这里暂时写死 
        Hr_emp emp = new Hr_emp();
        emp.setEmpId(1);
        client.setEmpId(emp);
        client.setCaseSituation("立案");
        client.setMessageId(message);
    }
    //修改客户信息
    @RequestMapping("updatemessage")
    public String updatemessage(String typeId,String name,String phone,String idnumber,String characteristics,String principal,
                                String salesman,String mailbox,String duty,String influence,String specialplane,String region,
                                String stateId,String legalperson,String legalpersonphone,String nation,String birthdate,String site,String remark,String sex){
        System.out.println("数据："+typeId);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Client_type clientType = clientService.getbytypeid(Integer.parseInt(typeId));
        Client_state clientState = clientService.getbystateid(Integer.parseInt(stateId));
        Client_message client_message = new Client_message();
        ParsePosition pos = new ParsePosition(0);
        Date date =formatter.parse(birthdate,pos);
        client_message.setBirthdate(date);
        client_message.setCharacteristics(characteristics);
        client_message.setDuty(duty);
        client_message.setIdnumber(idnumber);
        client_message.setInfluence(influence);
        client_message.setLegalperson(legalperson);
        client_message.setLegalpersonphone(legalpersonphone);
        client_message.setMailbox(mailbox);
        client_message.setName(name);
        client_message.setNation(nation);
        client_message.setPhone(phone);
        client_message.setPrincipal(principal);
        client_message.setTypeId(clientType);
        client_message.setStateId(clientState);
        client_message.setSpecialplane(specialplane);
        client_message.setSite(site);
        client_message.setSex(sex);
        client_message.setSalesman(salesman);
        client_message.setRegion(region);
        client_message.setRemark(remark);
        clientService.updatemessage(client_message);
        System.out.println("修改完成！");
        return "alter";
    }
}
